#!/usr/bin/env bash

default_distributionUrl="https://github.com/flutter/flutter"
#git地址：https://github.com/flutter/flutter.git 如果下载太慢可以使用 https://gitee.com/mirrors/Flutter.git
default_flutterChannel="stable"
#master分支上version tag是最全的
#版本查询地址：https://flutter.cn/docs/development/tools/sdk/releases






# 读取环境变量，定位flutter sdk的位置
path=$PATH
flutter_path=''
array=(${path//:/ })
for var in ${array[@]}
do
    if [[ $var =~ "flutter/bin" ]]
    then
        flutter_path=$var
    fi
done

if [ "$flutter_path" !=  "" ]
then
    download_dir=${flutter_path%/*}
else
    bash_path=$(cd `dirname $0`; pwd) # work_path=$(pwd)
    download_dir="$bash_path/.flutter"
fi


# 读取 flutter-wrapper.properties 中的配置
properties=''
if test -e 'flutter/wrapper/flutter-wrapper.properties'
then
    properties='flutter/wrapper/flutter-wrapper.properties'
else
    echo '请确认 flutter/wrapper/flutter-wrapper.properties 是否存在'
    exit -1
fi

distributionUrl=""
flutterVersion=""
flutterChannel=""

IFS=$'\n'

for line in `cat $properties`
do
    # 忽略注释
    comment=`echo "$line" | grep '^#'`
    if [ -n "$comment" ]
    then
        echo "忽略注释"
        continue
    fi
    key=`echo $line | cut -d '=' -f1`
    value=`echo $line | cut -d '=' -f2`
    case $key in
    "distributionUrl")
        distributionUrl="$value"
        ;;
    "flutterVersion")
        flutterVersion="$value"
        ;;
    "flutterChannel")
        flutterChannel="$value"
        ;;
    *)
        echo "$key 选项不被支持，请确认"
        ;;
    esac
done

if [  -z $distributionUrl ]
then
    echo "distributionUrl 为空，将使用默认值 $default_distributionUrl"
    distributionUrl=default_distributionUrl
fi

if [ -z $flutterVersion ]
then
    echo "必须指定 flutterVersion"
    exit -1
fi

if [ -z $flutterChannel ]
then
    echo "flutterChannel 为空，将使用默认值 $default_flutterChannel"
    flutterChannel=default_flutterChannel
fi

# 初始化命令
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
if [[ "$(uname)" == *NT* ]] #判断是否是Windows系统，嵌套中括号是支持正则使用
then
    flutter_command="$download_dir/bin/flutter.bat"
    dart_command="$download_dir/bin/cache/dart-sdk/bin/dart.exe"
else
    flutter_command="$download_dir/bin/flutter"
    dart_command="$download_dir/bin/cache/dart-sdk/bin/dart"
fi

# 执行方法封装，支持flutterw dart 来运行dart文件
execFlutter() {
if [[ $1 == "dart" ]]
then
    str=$*
    substr=${str:4}
    $dart_command $substr
else
    $flutter_command $*
fi
}

# 更新dart sdk、处理 pub
handleSDK() {
	# 下载 或 更新 dart sdk
    $flutter_command doctor
    # 获取版本号
    curFlutterVersion=`$flutter_command --version | grep '^Flutter' | cut -d ' ' -f2`
    # 更新.android、.ios内部的flutter sdk路径
    $flutter_command pub get
    echo "===SDK handle success：$curFlutterVersion"
}

# 下载 https://ec2-13-250-177-223.ap-southeast-1.compute.amazonaws.com
if [ ! -d $download_dir ]
then
    echo "===Create $download_dir dir"
    mkdir $download_dir
fi

if [ ! -r $flutter_command ]
then
    echo "===Start download SDK：git clone -b $flutterChannel $distributionUrl $download_dir"
    git clone -b $flutterChannel $distributionUrl $download_dir
    if [ -r $flutter_command ]
    then
        handleSDK
    else
        echo "===SDK download failed"
        exit -1
    fi
fi

# 切换版本
curFlutterVersion=`$flutter_command --version | grep '^Flutter' | cut -d ' ' -f2`
if [ $curFlutterVersion == $flutterVersion ]
then
    execFlutter $*
else
    echo "===Current version：$curFlutterVersion，Target version：$flutterVersion"
	################################################################ 切换channel
    (cd $download_dir && git reset --hard HEAD)
    $flutter_command channel $flutterChannel
	#echo "===Upgrading"
    #$flutter_command upgrade -f
	################################################################ 切换版本
	echo "===Checkout version"
	(cd $download_dir && git reset --hard HEAD)
	(cd $download_dir && git checkout "v$flutterVersion")
    #$flutter_command downgrade "v$flutterVersion"
	echo "===Checkout version done"
	################################################################
	# 预处理SDK
	handleSDK
    # 判断切换版本是否成功
    curFlutterVersion=`$flutter_command --version | grep '^Flutter' | cut -d ' ' -f2`
    if [ $curFlutterVersion == $flutterVersion ]
    then
        execFlutter $*
    else
        echo "===Change version failed"
    fi
fi
# 备注
# 获取某个命令的返回值 使用 STR=$(uname) 或者 STR=`uname`
# 变量赋值等号前后不能有空格 如 STR="123"
# shell if 是对空格敏感 如：if [ "dd" == "dd" ]
# if [[ "$(uname)" == *AA* ]] 嵌套中括号是支持正则使用